package com.atguigu.controller;

import com.atguigu.entity.product.Category;
import com.atguigu.service.CategoryService;
import com.atguigu.vo.common.Result;
import com.atguigu.vo.common.ResultCodeEnum;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Tag(name = "分类")
@RestController
@RequestMapping("/product/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @Operation(summary = "列表")
    @GetMapping("/findList")
    public Result findList(@RequestParam(required = false) Long categoryId) {
        if (categoryId == null) {
            categoryId = 0l;
        }
        List<Category> categoryList = categoryService.findListByParentId(categoryId);
        return Result.build(categoryList, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "导出")
    @GetMapping("/download")
    public void download(HttpServletResponse response){
        categoryService.download(response);
    }


    @Operation(summary = "导入")
    @GetMapping("/upload")
    public void upload(MultipartFile file){
        categoryService.upload(file);
    }
}
